//
//  RegisterControl.m
//  FreeTime
//
//  Created by  on 16/8/29.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "RegisterControl.h"
#import "Header.h"
#import "FirstInfoControl.h"

@interface RegisterControl (){
    UIButton * sendCode;
}

@end

@implementation RegisterControl

- (void)viewDidLoad {
    [self setupNav];
    [self setupUI];
}
#pragma mark -- selfStyle --
- (void)setupUI{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    NSArray * arr = @[@"请输入您的手机号",@"请输入您的验证码",@"设置您的密码",@"确认您的密码"];
    CGFloat y = 0;
    for (int i =0; i<arr.count; i++) {
        UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 5+i*44, WindowWidth-20, 44)];
        if (i == 0) {
            sendCode = [UIButton buttonWithType:UIButtonTypeCustom];
            sendCode.frame = CGRectMake((WindowWidth-20)/3*2+1, 0, (WindowWidth-20)/3, 44);
            [sendCode addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];
            sendCode.titleLabel.font = FONT(12);
            [sendCode setTitle:@"发送验证码" forState:UIControlStateNormal];
            [sendCode setTitleColor:[ImageUtil colorWithRgb:0x666666] forState:UIControlStateNormal];
            tf.delegate = self;
            [tf addSubview:sendCode];
            
            [ToolUtil createVerticalLine:6 superView:tf lift:(WindowWidth-20)/3*2 height:25 color:[ImageUtil colorWithRgb:0xe6e6e6]];
            
        }
        tf.borderStyle = UITextBorderStyleNone;
        tf.font = FONT(14);
        [ToolUtil createLine:40 superView:tf lift:0 width:WindowWidth-20 hight:1 color:[ImageUtil colorWithRgb:0xcccccc]];
        tf.placeholder = arr[i];
        [self.view addSubview:tf];
        y = CGRectGetMaxY(tf.frame);
    }
    UIButton * enterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    enterBtn.frame = CGRectMake(60, y+45, WindowWidth-120, 44);
    [enterBtn addTarget:self action:@selector(enterBtn) forControlEvents:UIControlEventTouchUpInside];
    //    enterBtn.backgroundColor = DEBUG_CLOLR;
    
    [enterBtn setBackgroundImage:[UIImage imageNamed:@"querenbiao"] forState:UIControlStateNormal];
    UILabel * titleLbl = [ReduceCode creatLabelWithFrame:CGRectMake((WindowWidth-120-80)/2, 0, 80, 40) title:@"完成" titleColor:[UIColor whiteColor] font:14 textAlignment:1];
    [enterBtn addSubview:titleLbl];
    enterBtn.titleLabel.font = FONT(17);
    [self.view addSubview:enterBtn];
}
#pragma mark -- 响应事件 --
- (void)enterBtn{
    [self.navigationController pushViewController:[FirstInfoControl new] animated:YES];
}
-(void)startTime{
    __block int timeout= 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [sendCode setTitle:@"重新发送验证码" forState:UIControlStateNormal];
                sendCode.userInteractionEnabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [sendCode setTitle:[NSString stringWithFormat:@"%zd秒后重新发送",timeout] forState:UIControlStateNormal];
                [UIView commitAnimations];
                sendCode.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
